package com.cxyd.modules.job.task.logicTask;

import com.cxyd.modules.sys.service.SysUserService;
import com.cxyd.modules.wms.dao.ErpLogDao;
import com.cxyd.modules.wms.dao.ErpReelDao;
import com.cxyd.modules.wms.dao.PostSapDao;
import com.cxyd.modules.wms.dto.PostSapDTO;
import com.cxyd.modules.wms.entity.ErpLogEntity;
import com.cxyd.modules.wms.entity.ErpReelEntity;
import com.cxyd.modules.wms.entity.PostSapEntity;
import com.cxyd.modules.wms.service.PostSapService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

@Component("post-sap")
@Slf4j
public class PostSapTask {
    private final SysUserService userService;
    private final PostSapService postSapService;
    private final PostSapDao postSapDao;
    private final ErpReelDao erpReelDao;
    private final ErpLogDao erplogDao;
    //创建锁，防止并发情况出现
    private Lock lock = new ReentrantLock();

    public PostSapTask(SysUserService userService, PostSapService postSapService, PostSapDao postSapDao, ErpReelDao erpReelDao, ErpLogDao erplogDao) {
        this.userService = userService;
        this.postSapService = postSapService;
        this.postSapDao = postSapDao;
        this.erpReelDao = erpReelDao;
        this.erplogDao = erplogDao;
    }

    public void scanPost() {
        lock.lock();
        try {
            //扫码是否有收货确认的数据
            List<PostSapEntity> records = postSapService.getRecords();
            if (records.isEmpty()) {
                log.info("无更新ERP的数据!");
                return;
            }

            for(PostSapEntity record:records)
            {
                try
                {
                    ErpLogEntity logInsert = new ErpLogEntity();
                    logInsert.setRid(record.getLgnum());
                    logInsert.setPn(record.getUname());
                    logInsert.setQty(record.getMenge().longValue());
                    logInsert.setEventid("LGRSTACG");
                    if(record.getMsgType().equals("IN"))
                    {
                        logInsert.setStation(record.getSrmdn());
                    }
                    else
                    {
                        logInsert.setStation(userService.get(record.getCreator()).getUsername());
                    }
                    logInsert.setCompany("PEGATRON");
                    logInsert.setRemarks(record.getComments());
                    logInsert.setUid(userService.get(record.getCreator()).getUsername());
                    erplogDao.insertLog(logInsert);

                    ErpReelEntity updateReel = new ErpReelEntity();
                    if(record.getMsgType().equals("IN"))
                    {
                        updateReel.setRstation(record.getSrmdn());
                    }
                    else
                    {
                        updateReel.setRstation(userService.get(record.getCreator()).getUsername());
                    }
                    updateReel.setRid(record.getLgnum());
                    erpReelDao.updateReel(updateReel);

                    record.setPostSt(1);
                    record.setUpdateDate(new Date());
                    record.setMsgContent("成功");

                }
                catch (Exception ex)
                {
                    record.setMsgContent(ex.toString());
                    record.setUpdateDate(new Date());

                }
                //更新結果
                postSapDao.updateById(record);

            }


        } catch (Exception e) {
            log.error("上报SAP收货确认异常，causeBy:{}", e.getMessage());
        } finally {
            lock.unlock();//释放锁
        }
    }
}
